/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NuevoPago.java
 *
 * Created on 22-may-2010, 21:24:43
 */

package ui;
import acceso.Pago;
import gestion.GestionSpring;
import interfaz.InterfaceEjp;
import javax.swing.JOptionPane;
import manejoExcepciones.ExcepcionesPago;

/**
 *
 * @author Gustavo
 */
public class NuevoPago extends javax.swing.JFrame {

       PaginaPrincipal miPaginaPrincipal;


   public void setMiPaginaPrincipal(PaginaPrincipal miPaginaPrincipal) {
        this.miPaginaPrincipal = miPaginaPrincipal;

    }


    /** Creates new form NuevoPago */
    public NuevoPago() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        NombreIngresar = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        ComboFrecuencia = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        BotonOkInsertar = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        Mensaje = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        MontoIngresar = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        FechaPagoIngresar = new com.toedter.calendar.JDateChooser();
        FechaCorteIngresar = new com.toedter.calendar.JDateChooser();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Inversiones E.J.P");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Nuevo Producto"));
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel1.setText("(*)Nombre:");
        jLabel1.setName("jLabel1"); // NOI18N

        NombreIngresar.setName("NombreIngresar"); // NOI18N
        NombreIngresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NombreIngresarActionPerformed(evt);
            }
        });

        jLabel2.setText("(*)Frecuencia:");
        jLabel2.setName("jLabel2"); // NOI18N

        ComboFrecuencia.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Mensual", "Trimestral", "Semestral", "Anual" }));
        ComboFrecuencia.setName("ComboFrecuencia"); // NOI18N
        ComboFrecuencia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ComboFrecuenciaActionPerformed(evt);
            }
        });

        jLabel4.setText("(*)Fecha pago:");
        jLabel4.setName("jLabel4"); // NOI18N

        BotonOkInsertar.setText("OK");
        BotonOkInsertar.setName("BotonOkInsertar"); // NOI18N
        BotonOkInsertar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonOkInsertarActionPerformed(evt);
            }
        });

        jButton2.setText("Cancelar");
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        Mensaje.setName("Mensaje"); // NOI18N

        jLabel5.setText("(*)Monto:");
        jLabel5.setName("jLabel5"); // NOI18N

        MontoIngresar.setName("MontoIngresar"); // NOI18N
        MontoIngresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MontoIngresarActionPerformed(evt);
            }
        });

        jLabel6.setText("(*)Fecha de corte:");
        jLabel6.setName("jLabel6"); // NOI18N

        FechaPagoIngresar.setName("FechaPagoIngresar"); // NOI18N

        FechaCorteIngresar.setName("FechaCorteIngresar"); // NOI18N
        FechaCorteIngresar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                FechaCorteIngresarMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                FechaCorteIngresarMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                FechaCorteIngresarMouseExited(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                FechaCorteIngresarMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                FechaCorteIngresarMouseReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(624, 624, 624)
                        .addComponent(Mensaje, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addGap(20, 20, 20)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(jLabel2)
                                        .addComponent(jLabel1)))
                                .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING))
                            .addComponent(jLabel6))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(NombreIngresar, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)
                            .addComponent(ComboFrecuencia, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(MontoIngresar)
                            .addComponent(FechaPagoIngresar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(BotonOkInsertar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton2))
                            .addComponent(FechaCorteIngresar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(NombreIngresar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(ComboFrecuencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(FechaCorteIngresar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(MontoIngresar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(FechaPagoIngresar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BotonOkInsertar)
                    .addComponent(jButton2))
                .addGap(67, 67, 67)
                .addComponent(Mensaje, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 237, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-326)/2, (screenSize.height-297)/2, 326, 297);
    }// </editor-fold>//GEN-END:initComponents

    private void NombreIngresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NombreIngresarActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_NombreIngresarActionPerformed

    private void ComboFrecuenciaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ComboFrecuenciaActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_ComboFrecuenciaActionPerformed


    private void BotonOkInsertarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonOkInsertarActionPerformed

        //Instanciaciamos la interface.
        InterfaceEjp miInterfaceEjp = GestionSpring.getSingleton().MetodoGestion();

        
      int FrecuenciaM = 0;

       int Seleccion = ComboFrecuencia.getSelectedIndex();
                              if (Seleccion == 0)
       {
           FrecuenciaM = 1;
       }

        if (Seleccion == 1)
       {
           FrecuenciaM = 3;
       }
         if (Seleccion == 2)
       {
          FrecuenciaM = 6;
       }
       else if (Seleccion == 3)
       {
          FrecuenciaM = 12;
       }


     try{
           
       //Verificamos si los campos obligatorios han sido llenados.
       manejoExcepciones.ExcepcionesPago.CamposVacios
       (NombreIngresar.getText(), FechaCorteIngresar.getDate()
       ,FechaPagoIngresar.getDate(), MontoIngresar.getText());

       //Verificamos que el monto es un valor numerico.
       float precio = Float.parseFloat(MontoIngresar.getText());

       //Verificamos que el monto no sea negativo
       manejoExcepciones.ExcepcionesPago.CampoNoNegativo(precio);

       //Verificamos si las fechas son validas, no antes de la actual
       manejoExcepciones.ExcepcionesPago.FechaValida(FechaCorteIngresar.getDate());
       manejoExcepciones.ExcepcionesPago.FechaValida(FechaPagoIngresar.getDate());

       //Construimos el objeto pago para ingresarlo a la BD.
       Pago miPago = new Pago(NombreIngresar.getText(),FechaCorteIngresar.getDate()
                        ,FechaPagoIngresar.getDate(), FrecuenciaM, 
                        Float.parseFloat(MontoIngresar.getText()));

       //Registramos el pago.
       Long a = miInterfaceEjp.registrarPago(miPago);
       //Enviamos mensaje de exito.
        JOptionPane.showMessageDialog(this,"Insercion Exitosa");
        //Refrescamos el JTable TablaPago.
        miPaginaPrincipal.Refrescar();

        this.setVisible(false);

       }catch (ExcepcionesPago ExI1){

         JOptionPane.showMessageDialog(this,ExI1.getMessage());


       }catch (NumberFormatException NFEI){

         JOptionPane.showMessageDialog(this,"EL campo monto debe ser numerico");
       }

        
}//GEN-LAST:event_BotonOkInsertarActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.setVisible(false);
}//GEN-LAST:event_jButton2ActionPerformed

    private void MontoIngresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MontoIngresarActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_MontoIngresarActionPerformed

    private void FechaCorteIngresarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_FechaCorteIngresarMouseClicked
        // TODO add your handling code here:
}//GEN-LAST:event_FechaCorteIngresarMouseClicked

    private void FechaCorteIngresarMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_FechaCorteIngresarMouseEntered
        // TODO add your handling code here:
}//GEN-LAST:event_FechaCorteIngresarMouseEntered

    private void FechaCorteIngresarMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_FechaCorteIngresarMouseExited
       // TODO add your handling code here:
}//GEN-LAST:event_FechaCorteIngresarMouseExited

    private void FechaCorteIngresarMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_FechaCorteIngresarMousePressed
        // TODO add your handling code here:
}//GEN-LAST:event_FechaCorteIngresarMousePressed

    private void FechaCorteIngresarMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_FechaCorteIngresarMouseReleased
        // TODO add your handling code here:
}//GEN-LAST:event_FechaCorteIngresarMouseReleased

 

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NuevoPago().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BotonOkInsertar;
    private javax.swing.JComboBox ComboFrecuencia;
    private com.toedter.calendar.JDateChooser FechaCorteIngresar;
    private com.toedter.calendar.JDateChooser FechaPagoIngresar;
    private javax.swing.JLabel Mensaje;
    private javax.swing.JTextField MontoIngresar;
    private javax.swing.JTextField NombreIngresar;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

}
